package com.blockmeta.bbs.immodule.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import androidx.annotation.Nullable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ImageSpan;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blockmeta.bbs.baselibrary.base.app.BaseApp;
import com.blockmeta.bbs.baselibrary.utils.loading.ProgressBarUtil;
import com.blockmeta.bbs.businesslibrary.net.pojo.BaseImResponse;
import com.blockmeta.bbs.businesslibrary.net.retrofit.RetrofitFactory;
import com.blockmeta.bbs.businesslibrary.net.rxjava.CommonSubscriber;
import com.blockmeta.bbs.businesslibrary.net.rxjava.RxUtil;
import com.blockmeta.bbs.immodule.R;
import com.blockmeta.bbs.immodule.net.api.ShieldUserApi;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

public class TempRelationBar extends LinearLayout {
    TextView mtvTemprelationAddfriend;

    TextView mtvTemprelationShield;

    private IMAddFriendMsg imPopAddFriendMsg;

    long mToUserId;

    private ITempRelationBarCallBack mCallBack;
    /**
     * true 为屏蔽
     * false 为为屏蔽
     */
    private Boolean shield;


    public TempRelationBar(Context context) {
        super(context);
        init(context);
    }

    public TempRelationBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TempRelationBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(final Context context){
        inflate(context, R.layout.view_temprelation_bar,this);
        mtvTemprelationAddfriend = (TextView)findViewById(R.id.tv_temprelation_addfriend);
        mtvTemprelationShield = (TextView)findViewById(R.id.tv_temprelation_shield);
        bindTempRelationActionBar();
        mtvTemprelationAddfriend.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                imPopAddFriendMsg = new IMAddFriendMsg(context, mToUserId + "");
                imPopAddFriendMsg.show();
            }
        });

        mtvTemprelationShield.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ProgressBarUtil.showProgress(context);
                RetrofitFactory.getRootRetrofit().create(ShieldUserApi.class)
                        .shieldUser(mToUserId, !shield)
                        .compose(RxUtil.dataNullResult())
                        .compose(RxUtil.<BaseImResponse<Object>>rxSchedulerHelper())
                        .compose(((RxAppCompatActivity) context).<BaseImResponse<Object>>bindToLifecycle())
                        .subscribe(new CommonSubscriber<BaseImResponse<Object>>(context) {
                            @Override
                            public void onNext(BaseImResponse<Object> o) {
                                if(mCallBack != null){
                                    shield = !shield;
                                    mCallBack.shieldSuccess(shield);
                                    setShieldStatus(shield);
                                }
                            }
                        });
            }
        });
    }

    public void onInitialize(long toUserId, ITempRelationBarCallBack iTempRelationBarCallBack) {
        mToUserId = toUserId;
        mCallBack = iTempRelationBarCallBack;
    }

    public void setShieldStatus(Boolean shield) {
        this.shield = shield;
        bindTempRelationShield(shield ? "   取消屏蔽" : "   屏蔽消息");
    }


    public interface ITempRelationBarCallBack{
        void shieldSuccess(boolean shield);
    }


    public void bindTempRelationActionBar() {
        bindTempRelationAddFriend();
        bindTempRelationShield("   屏蔽消息");
    }

    public void bindTempRelationShield(String str) {
        SpannableString spannableString;
        Drawable tPeriodsDrawable;
        int tPeriodsTextSize;
        int tPeriodsHeight;
        int tPeriodsWidth;
        spannableString = new SpannableString(str);
        tPeriodsDrawable = BaseApp.getApp().getResources().getDrawable(R.drawable.icon_im_shield);
        tPeriodsTextSize = (int) mtvTemprelationAddfriend.getTextSize();
        tPeriodsHeight = tPeriodsTextSize;
        tPeriodsWidth = tPeriodsDrawable.getIntrinsicWidth() * tPeriodsTextSize / tPeriodsDrawable.getIntrinsicHeight();
        tPeriodsDrawable.setBounds(0, 0, tPeriodsWidth, tPeriodsHeight);
        spannableString.setSpan(new ImageSpan(tPeriodsDrawable, ImageSpan.ALIGN_BASELINE), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        mtvTemprelationShield.setText(spannableString);
    }

    public void bindTempRelationAddFriend() {
        SpannableString spannableString = new SpannableString("   加为好友");
        Drawable tPeriodsDrawable = BaseApp.getApp().getResources().getDrawable(R.drawable.icon_im_addfriend);
        int tPeriodsTextSize = (int) mtvTemprelationAddfriend.getTextSize();
        int tPeriodsHeight = tPeriodsTextSize;
        int tPeriodsWidth = tPeriodsDrawable.getIntrinsicWidth() * tPeriodsTextSize / tPeriodsDrawable.getIntrinsicHeight();
        tPeriodsDrawable.setBounds(0, 0, tPeriodsWidth, tPeriodsHeight);
        spannableString.setSpan(new ImageSpan(tPeriodsDrawable, ImageSpan.ALIGN_BASELINE), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        mtvTemprelationAddfriend.setText(spannableString);
    }
}
